//
//  AuthDetalViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/6/30.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "AuthDetalViewController.h"
#import "AppDelegate.h"
#import "KJSRequest.h"
#import "CommonUtil.h"

@interface AuthDetalViewController ()
@property (nonatomic,weak) IBOutlet UIButton *submintBtn;
@property (nonatomic,weak) IBOutlet UITextField *pNameTF;
@property (nonatomic,weak) IBOutlet UITextField *pIdNum;
@end

@implementation AuthDetalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"实名认证"];
    self.submintBtn.layer.cornerRadius = 10;
    self.submintBtn.layer.masksToBounds = YES;
    
    [self.submintBtn addTarget:self action:@selector(submitAuth) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - custom method
- (void)submitAuth
{
    if ([self.pNameTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入您的姓名" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateName:self.pNameTF.text]) {
        [GUATool showDialog:@"姓名应为中文" message:nil waitingTime:1.5];
    }else if ([self.pIdNum.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入您的身份证号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateIdentityCard:self.pIdNum.text]) {
        [GUATool showDialog:@"请正确输入身份证号" message:nil waitingTime:1.5];
    }else if (CurentDelegate.account.partyId) {
        if ([self.auditingOpinion isEqualToString:@"0"]) {
            [KJSRequest PersonReRealAuthWithPartyId:CurentDelegate.account.partyId pName:self.pNameTF.text pIdNum:self.pIdNum.text ret:^(BasicResult *model) {
                if (model.success) {
                    [GUATool showDialog:@"实名认证成功" message:nil waitingTime:1.5];
                    if ([CurentDelegate.account.levels integerValue] < 1) {
                        CurentDelegate.account.levels = @"1";
                        CurentDelegate.account.type = @"p";
                        CurentDelegate.account.nickName = self.pNameTF.text;
                        [self dismiss:YES];
                        [self performSelector:@selector(returnFunction) withObject:nil afterDelay:1.5f];
                    }
                }
            }];

        }else{
            [KJSRequest PersonRealAuthWithPartyId:CurentDelegate.account.partyId pName:self.pNameTF.text pIdNum:self.pIdNum.text ret:^(BasicResult *model) {
                if (model.success) {
                    [GUATool showDialog:@"实名认证成功" message:nil waitingTime:1.5];
                    if ([CurentDelegate.account.levels integerValue] < 1) {
                        CurentDelegate.account.levels = @"1";
                        CurentDelegate.account.type = @"p";
                        CurentDelegate.account.nickName = self.pNameTF.text;
                        [self dismiss:YES];
                        [self performSelector:@selector(returnFunction) withObject:nil afterDelay:1.5f];
                    }
                }
            }];
        }
    }else{
        [GUATool showDialog:@"请登录" message:nil waitingTime:1.5];
    }
}
-(void)returnFunction
{
    if (self.authSuccess) {
        self.authSuccess();
    }
}

@end
